Ever wished you could customize your burger exactly the way you like it—no shortcuts, no mix-ups? That’s exactly what the Builder Pattern does for your code. It lets you construct a complex object step by step, just like building your perfect burger from scratch.
Why Bother with It?
- Step-by-Step Customization: Instead of trying to whip up a burger in one go, you build it piece by piece. Want extra toppings or a different sauce? No problem.
- Keep It Flexible: You separate the burger-building process from the final product. Your code gets a ready-made burger without needing to know how every layer was added.
- Easy to Change: Craving a veggie burger instead of beef? Simply swap out the builder, and you’re done—no need to rewrite your whole order.
How It Works (In Plain English)
- Lay Out the Steps: Define a builder interface that lists all the steps to create your burger (like placing the bottom bun, adding the patty, toppings, sauce, and finally, the top bun).
- Build Your Burgers: Create concrete builders (like a ClassicBurgerBuilder) that implement these steps in their own way.
- The Burger Chef: Use a director—think of it as your personal burger chef—who calls the builder’s steps in order to assemble your burger.
- Enjoy Your Burger: The client simply asks the chef to make a burger. It doesn’t worry about how each layer is added—it just gets a delicious, custom-built burger.
C# Code Example
C#
using System;
// The final product: your burger
public class Burger
{
public string BottomBun { get; set; }
public string Patty { get; set; }
public string Toppings { get; set; }
public string Sauce { get; set; }
public string TopBun { get; set; }
public void ShowBurger()
{
Console.WriteLine("Your Burger:");
Console.WriteLine(" Bottom Bun: " + BottomBun);
Console.WriteLine(" Patty: " + Patty);
Console.WriteLine(" Toppings: " + Toppings);
Console.WriteLine(" Sauce: " + Sauce);
Console.WriteLine(" Top Bun: " + TopBun);
}
}
// Builder interface listing the burger-building steps
public interface IBurgerBuilder
{
void BuildBottomBun();
void BuildPatty();
void AddToppings();
void AddSauce();
void BuildTopBun();
Burger GetBurger();
}
// Concrete Builder: builds a classic burger
public class ClassicBurgerBuilder : IBurgerBuilder
{
private Burger burger = new Burger();
public void BuildBottomBun() => burger.BottomBun = "Sesame Bottom Bun";
public void BuildPatty() => burger.Patty = "Beef Patty";
public void AddToppings() => burger.Toppings = "Lettuce, Tomato, Pickles";
public void AddSauce() => burger.Sauce = "Ketchup & Mustard";
public void BuildTopBun() => burger.TopBun = "Sesame Top Bun";
public Burger GetBurger() => burger;
}
// Director: the burger chef who orchestrates the building process
public class BurgerChef
{
private IBurgerBuilder builder;
public BurgerChef(IBurgerBuilder builder)
{
this.builder = builder;
}
public Burger MakeBurger()
{
builder.BuildBottomBun();
builder.BuildPatty();
builder.AddToppings();
builder.AddSauce();
builder.BuildTopBun();
return builder.GetBurger();
}
}
// Client code: ordering your custom burger
public class Program
{
public static void Main()
{
IBurgerBuilder burgerBuilder = new ClassicBurgerBuilder();
BurgerChef chef = new BurgerChef(burgerBuilder);
Burger burger = chef.MakeBurger();
burger.ShowBurger();
}
}
The Takeaway
The Builder Pattern is like having your very own burger chef. You decide how each layer of your burger comes together, and the chef takes care of assembling it in the perfect order. This makes your code flexible, customizable, and super easy to extend—just like switching from a beef burger to a veggie delight without any fuss. Enjoy building your perfect burger, and happy coding!